package com.googlecode.gamedroid.event;

import java.nio.ByteBuffer;

public class Event
{
	private static final short AMPLITUDE = 32767;
	private static ByteBuffer buffer = ByteBuffer.allocate(5); // so it can be reused
	
	private short mKey;
	private byte mType;
	private short mValue = 0;
	
	public Event(byte type, short key)
	{
		this.mType = type;
		this.mKey = key;
	}
	
	public byte[] bytes()
	{
		buffer.clear();
		buffer.put(mType);
		buffer.putShort(mKey);
		buffer.putShort(mValue);
		return buffer.array();
	}
	
	public short getKey()
	{
		return mKey;
	}
	
	public byte getType()
	{
		return mType;
	}
	
	public short getValue()
	{
		return mValue;
	}
	
	public void setKey(short key)
	{
		mKey = key;
	}
	
	/*
	 * set the value of the <Event> to <value>. <value> should be between -1.0 and 1.0
	 */
	public void setValue(double value)
	{
		mValue = (short) Math.min(Event.AMPLITUDE, Math.max(-Event.AMPLITUDE, value * Event.AMPLITUDE));
	}
	
	/*
	 * set the value of the <Event> to <value>. <value> should be between -32767 and 32767
	 */
	public void setValue(short value)
	{
		mValue = (short) Math.min(Event.AMPLITUDE, Math.max(-Event.AMPLITUDE, value));
	}
}
